﻿namespace Uber.Data
{
    using System.Data;
    using System.Data.SqlClient;
    using Core.Model;
    using Core.Repository;

    public class ShowRepo : BaseRepo, IShowRepo
    {
        public ShowRepo(IConnectionFactory connFactory) : base(connFactory)
        {
        }

        public Show Get(string name)
        {
            using (var conn = new SqlConnection(Cs))
            {
                using (var cmd = conn.CreateCommand())
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.CommandText = "getShowByName";
                    cmd.Parameters.Add("name", SqlDbType.NVarChar, 20).Value = name;
                    conn.Open();

                    using (var dr = cmd.ExecuteReader())
                    {
                        while (dr.Read())
                        {
                            var o = new Show();
                            o.Pro = name;
                            o.Cid = dr.GetInt32(0);
                            return o;
                        }
                        return null;
                    }
                }
            }
        }
        
        public Show Get(int cid)
        {
            using (var conn = new SqlConnection(Cs))
            {
                using (var cmd = conn.CreateCommand())
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.CommandText = "getShowByCid";
                    cmd.Parameters.Add("Cid", SqlDbType.Int).Value = cid;
                    conn.Open();

                    using (var dr = cmd.ExecuteReader())
                    {
                        while (dr.Read())
                        {
                            var o = new Show();
                            o.Pro = dr.GetString(0); 
                            o.Cid = cid;
                            return o;
                        }
                        return null;
                    }
                }
            }
        }
    }
}